//
//  ViewController.m
//  15qq
//
//  Created by apple on 15/8/4.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "ViewController.h"
#import "LMQQCell.h"
#import "LMQQFrame.h"
#import "LMQQ.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic,strong) NSMutableArray *qqFrame;
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet UITextField *textfield;

@end

@implementation ViewController

-(NSMutableArray *)qqFrame
{
    if (_qqFrame ==nil) {
        NSMutableArray *qqlist = [LMQQ qqToList];
        
        NSMutableArray *tmplist = [NSMutableArray array];
        
        
        for (LMQQ *qq in qqlist) {
            
            LMQQFrame *qqfame = [[LMQQFrame alloc] init];
            
            qqfame.qq = qq;
            [tmplist addObject:qqfame];
            
        }
        _qqFrame =tmplist;
        
    }
    return _qqFrame;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableview.allowsSelection =NO;
   // self.tableview.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    self.tableview.showsVerticalScrollIndicator =NO;
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(willchangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    self.textfield.delegate = self;
    
}

-(void)willchangeFrame:(NSNotification *) notification
{
   
    
    
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    CGRect frameEnd = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    CGFloat y = frameEnd.origin.y - self.view.frame.size.height;
    
    
    [UIView animateKeyframesWithDuration:duration delay:0.0 options:7<<16 animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, y);
        
    } completion:^(BOOL finished) {
        
    }];
    
    /*
     {
     //动画播放的频率
     UIKeyboardAnimationCurveUserInfoKey = 7;
     //播放的时间
     UIKeyboardAnimationDurationUserInfoKey = "0.25";
     //键盘大小
     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
     //中心点的键盘开始的坐标
     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 676}";
     //中心点的键盘结束的坐标
     UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}";
     //键盘移动开始的位置
     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 216}}";
     //键盘移动结束的位置
     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";
     */
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.qqFrame.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LMQQCell *cell = [LMQQCell qqcellwithTableView:tableView];
    
    LMQQFrame *frame = self.qqFrame[indexPath.row];
    
    
    cell.qqframe = frame;
    cell.contentView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    return cell;
}
#pragma -mark - 代理

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LMQQFrame *frame = self.qqFrame[indexPath.row];
 
    return  frame.rowHeight;
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
   
    [self addmessage:textField.text type:metype];
    
    NSString *otherstr =[NSString stringWithFormat:@"%@你个蛋子",textField.text];
    
    [self addmessage:otherstr type:othertype];
    
    
    [self.tableview reloadData];
    
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.qqFrame.count-1 inSection:0];
    
    [self.tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
     
    
    textField.text =nil;
    
    return  YES;
}


- (void) addmessage:(NSString *)content type:(qqtype) type
{
    //获取上一个模型
    LMQQ *lastqq = [[self.qqFrame lastObject] qq];
    
    LMQQ *newqq = [[LMQQ alloc] init];
    
    newqq.text = content;
    newqq.time = @"今天 12:12";
    newqq.type = type;
    newqq.hiddenTime = [lastqq.time isEqualToString:newqq.time];
    
    LMQQFrame *frame = [[LMQQFrame alloc] init];
    frame.qq = newqq;
    
    [self.qqFrame addObject:frame];
}
@end
